package com.example.yuntiku.dao.impl;

import com.example.yuntiku.dao.BaseDao;
import com.example.yuntiku.dao.PagerDao;
import com.example.yuntiku.dao.ProductDao;
import com.example.yuntiku.entity.Curriculum;
import com.example.yuntiku.entity.Product;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductDaoImpl extends BaseDao implements ProductDao {
    public List<Product> selectAll(){
        List<Product> list = new ArrayList<>();
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            conn = super.getConnection();
            stmt = conn.prepareStatement("SELECT pname, ptype, parent_id FROM `product` WHERE ptype != 0");
            rs = stmt.executeQuery();
            while (rs.next()){
                Product product = new Product();
                product.setPname(rs.getString("pname"));
                product.setPtype(rs.getInt("ptype"));
                product.setParentid(rs.getInt("parent_id"));
                list.add(product);
            }
        }catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            super.closeAll(conn,stmt,rs);
        }
        return list;
    }
    public List<Product> selectZji(int ptype){
        List<Product> list = new ArrayList<>();
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            conn = super.getConnection();
            stmt = conn.prepareStatement("SELECT pname, ptype,parent_id FROM `product` WHERE `parent_id`=?");
            stmt.setInt(1,ptype);
            rs = stmt.executeQuery();
            while (rs.next()){
                Product product = new Product();
                product.setPname(rs.getString("pname"));
                product.setPtype(rs.getInt("ptype"));
                product.setParentid(rs.getInt("parent_id"));
                list.add(product);
            }
        }catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            super.closeAll(conn,stmt,rs);
        }
        return list;
    }

}
